package com.example.day01_homework;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_post;

    private String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        btn_post = (Button) findViewById(R.id.btn_post);
        btn_post.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_post:
                initData();
                break;
        }
    }

    private void initData() {
        //username,password,repassword
        OkHttpClient okHttpClient = new OkHttpClient();
        FormBody build1 = new FormBody.Builder()
                .add("username", "哒哒哒")
                .add("password", "111111")
                .add("repassword", "111111").build();
        Request build = new Request.Builder().post(build1).url("https://www.wanandroid.com/user/register").build();
        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: ");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
            Log.d(TAG,"onResponse: " + response.body().string());
            }
        });
    }
}
